package com.gs.cd.service.resource.api;

//auth: seven
//
//date: 2020/12/22 14:38:00

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.gs.cd.api.ApiResult;
import com.gs.cd.model.resource.SysResource;
import com.gs.cd.model.resource.SysResourceGroup;
import com.gs.cd.utils.api.ApiResultParserUtils;
import com.gs.cd.utils.url.UrlUtils;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import org.springframework.lang.Nullable;

import java.util.*;

@Log4j2
public class ResourceApi {
    private final static String rootUri = "v1/resource/";

    private static String url;

    public static String getUrl() {
        return url;
    }


    public ResourceApi(String service, String saleCode) {
        url = UrlUtils.getUrl(service, rootUri, saleCode);
        log.debug("初始化资源服务地址" + url);
        try {
            String s = HttpUtil.get(service, 3000);
            ApiResult apiResult = JSON.parseObject(s, ApiResult.class);
            if (apiResult.suc()) {
                log.info("完成初始化资源服务");
            } else {
                log.error(String.format("%s 资源服务 状态错误：%s", service, s));
            }
        } catch (Exception e) {
            log.error(String.format("%s 资源服务请求错误", service));
        }
    }


    /**
     * [资源]获取树结构的资源
     */
    public List<SysResource> treeListAllResource() {
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resource/listTree"));
        return ApiResultParserUtils.parseList(s, SysResource.class);
    }

    /**
     * [资源]获取资源
     */
    public List<SysResource> listAllResourceGroup(SysResource condition) {
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resource/list"), UrlUtils.params(condition));
        return ApiResultParserUtils.parseList(s, SysResource.class);
    }


    /**
     * [资源] 获取根资源
     *
     * @return
     */
    public List<SysResource> listRootResource() {
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resource/listRoot"));
        return ApiResultParserUtils.parseList(s, SysResource.class);
    }

    /**
     * [资源组]获取树结构的资源
     */
    public List<SysResourceGroup> treeListAllResourceGroup() {
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resourceGroup/listTree"));
        return ApiResultParserUtils.parseList(s, SysResourceGroup.class);
    }

    /**
     * [资源组]获取资源
     */
    public List<SysResourceGroup> listAllResourceGroup(SysResourceGroup condition) {
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resourceGroup/list"), UrlUtils.params(condition));
        return ApiResultParserUtils.parseList(s, SysResourceGroup.class);
    }

    /**
     * [资源组] 获取根资源组
     *
     * @return
     */
    public List<SysResourceGroup> listRootResourceGroup() {
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resourceGroup/listRoot"));
        return ApiResultParserUtils.parseList(s, SysResourceGroup.class);
    }

    /**
     * 通过资源组 ids 获取相应资源
     * ids 为空表示查询所有
     */
    public List<SysResourceGroup> resourceByGroupIds(@NonNull Collection<String> ids) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("ids", ids);
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resourceGroup/resource/groupIds"), objectObjectHashMap);
        return ApiResultParserUtils.parseList(s, SysResourceGroup.class);
    }

    /**
     * 通过资源组codes 获取相应资源
     *
     * @return
     */
    public List<SysResourceGroup> resourceByGroupCodes(@NonNull Collection<String> codes) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("codes", codes);
        String s = HttpUtil.get(UrlUtils.getUrl(url, "resourceGroup/resource/groupCodes"), objectObjectHashMap);
        return ApiResultParserUtils.parseList(s, SysResourceGroup.class);
    }

}
